package controllers;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import models.Rubro;
import models.Subrubro;
import play.data.binding.Binder;
import play.db.Model;
import play.exceptions.TemplateNotFoundException;
import play.mvc.Controller;
import play.mvc.With;
import controllers.CRUD.ObjectType;

@With(Secure.class)
public class Subrubros extends CRUD {

	public static void create() throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type); 
        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Long sectorId= new Long(params.get("sectorId"));
        Long selectedRama= new Long(params.get("ramaId"));
        
        Model object = (Model) constructor.newInstance();
        Binder.bindBean(params.getRootParamNode(), "object", object);
        Long selectedRubro= new Long(params.get("rubroId"));
        
        ((Subrubro)object).rubro = Rubro.findById(selectedRubro);
        ((Subrubro)object).descripcion = params.get("subrubro.descripcion");
        validation.valid(object);
        if (validation.hasErrors()) {
        	flash.error("Error en los datos, intente nuevamente");
        	redirect("Sectores.show", sectorId, null);
        }
        object._save();
        
        redirect("Sectores.show", sectorId, selectedRama, selectedRubro);
    }
	
	public static List<Subrubro> getSubrubros(String rubro) {
		List<Subrubro> subrubros = new ArrayList<Subrubro>();
		if(!rubro.equals("")) {
			subrubros = Subrubro.find("rubro.id = ?", Long.parseLong(rubro)).fetch();
		}
		return subrubros;
	}
	
	public static void getSubrubrosJson(String rubro) {
		String[] result = new String[2];
		
		List<Subrubro> subrubros = new ArrayList<Subrubro>();
		
		if(!rubro.equals("")) {
			subrubros = Subrubro.find("rubro.id = ?", Long.parseLong(rubro)).fetch();
		}
		
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		String json = gson.toJson(subrubros);
		
		result[0] = "OK";
    	result[1] = json;
    	renderJSON(result);
	}
	
	public static void save(String id) throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Model object = type.findById(id);
        notFoundIfNull(object);
        Binder.bindBean(params.getRootParamNode(), "object", object);
        validation.valid(object);
        if (validation.hasErrors()) {
            renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/show.html", type, object);
            } catch (TemplateNotFoundException e) {
                render("CRUD/show.html", type, object);
            }
        }
        object._save();
        String rubro = ((Subrubro)object).rubro._key().toString();
        String rama = ((Subrubro)object).rubro.rama._key().toString();
        redirect("Sectores.show", ((Subrubro)object).rubro.rama.sector._key(), rama, rubro);
    }
}
